package com.foreveross.proxyip.infra.utils;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
/**
 * 类的泛型辅助类
 * @author xiangsf 2013-1-18
 *
 */
public final class GenericsUtils {
	
	public static <T> Class<T> getGenericClass(Class<?> clazz) {
		return getGenericClass(clazz, 0);
	}

	public static <T> Class<T> getGenericClass(Class<?> clazz, int index) {
		Type genType = clazz.getGenericSuperclass();
		
		if (genType instanceof ParameterizedType) {
			Type[] params = ((ParameterizedType) genType).getActualTypeArguments();

			if ((params != null) && (params.length >= (index - 1))) {
				return (Class<T>) params[index];
			}
		}
		return null;
	}
}
